﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using DocumentFormat.OpenXml.Office2010.Excel;
using DotNet.Utilities;
using JinfuyuanProject.Entity;
using JinfuyuanProject.Entity.Filter;
using JinfuyuanProject.Entity.ViewModels;
using JinfuyuanProject.Service;
using JinfuyuanProject.Service.Interface;

namespace JinfuyuanProject.Controllers
{
    /*
     * 2025-4-21 采购 入库，
     * 2025-4-23出库，出入库记录
     * 商品页面
     * 赵莹
     */
    public class ProductController : Controller
    {
        JinfuyuanDbContext db = new JinfuyuanDbContext();
        private readonly IProductService _productService;
        private readonly IProcurementsService _procurementsService;
        private readonly IStaffService _staffService;
        private readonly IInboundService _inboundService;
        private readonly IContractService _contractService;
        private readonly JinfuyuanDbContext _dbContext;
        private readonly IProductApplicationService _productApplication;


        // GET: Product
        public ProductController(IProductService productService,IProcurementsService procurementsService,IStaffService staffService,IInboundService inboundService,IContractService contractService, JinfuyuanDbContext db,IProductApplicationService productApplication)
        {
            _productService = productService;
            _procurementsService = procurementsService;
            _staffService = staffService;
            _inboundService=inboundService;
            _contractService = contractService;
            _dbContext = db;
            _productApplication= productApplication;
        }

        #region 展示页面
        public ActionResult Index()
        {
            return View();
        }
        //商品详情
        public ActionResult ProductList()
        {
            return View();
        }
        //添加页面
        public ActionResult Add()
        {

            return View();
        }
        //采购详情
        public ActionResult Detail(int id)
        {
            ViewBag.Id = id;
            return View();
        }
        //零售价格修改
        public ActionResult EditPrice(int id)
        {
            ViewBag.Id = id;
            return View();
        }
        //出库
        /*
         2025-4-25
         新任务
         */
        //向OutProduct页面传输数据
        public ActionResult OutProduct(string data)
        {
            string decodedData = HttpUtility.UrlDecode(data);
            var model = Newtonsoft.Json.JsonConvert.DeserializeObject<List<InboundVM>>(decodedData);
            return View(model);
        }

        //出入库记录页面
        public ActionResult InOutList()
        {
            return View();
        }
        #endregion


        #region 接口
        //获得数据分页
        public JsonResult ProductContent(int page = 1, int limit = 10, string productName = null)
        {
            ProductFilter filter = new ProductFilter();
            filter.page = page;
            filter.limit = limit;
            filter.ProductName = productName;
            filter.IsDelete = 0;
            // 返回数据给前端
            return Json(_productService.Query(filter), JsonRequestBehavior.AllowGet);
        }
        //获得采购详情（批次表数据）
        public JsonResult ProcurementsContent(int Id)
        {
            Hashtable hashtable = new Hashtable();
            List<Procurements> procurements = _procurementsService.GetList().Where(p => p.ProductId == Id).ToList();
            hashtable["code"] = 0;
            hashtable["message"] = "success";
            hashtable["data"] = procurements;
            return Json(hashtable, JsonRequestBehavior.AllowGet);
        }
        //新商品添加
        public JsonResult ProductInsert(string productData)
        {
            productData = HttpUtility.UrlDecode(productData);
            ProductVM productVm = Newtonsoft.Json.JsonConvert.DeserializeObject<ProductVM>(productData);
            return Json(_productService.NewProductProcurement(productVm, ((Staff)Session["user"]).Id), JsonRequestBehavior.AllowGet);
        }
        //添加入库信息
        public JsonResult InboundInsert(string productData)
        {
            // 反序列化 JSON 字符串为 List<InboundVM> 对象
            productData = HttpUtility.UrlDecode(productData);
            ProductVM Data = Newtonsoft.Json.JsonConvert.DeserializeObject<ProductVM>(productData);
            return Json(_productService.CreateInbound(Data, ((Staff)Session["user"]).Id), JsonRequestBehavior.AllowGet);
        }
        //得到零售价
        public JsonResult GetPrice(int Id)
        {
            Hashtable ht = new Hashtable();
            Product product = db.Product.Find(Id);
            if (product == null)
                ht["code"] = 1;
            ht["code"] = 0;
            ht["data"] = product;
            return Json(ht);
        }
        //更新零售价
        public JsonResult UpdatePrice(string productData)
        {
            Hashtable ht = new Hashtable();
            if (!ModelState.IsValid)
            {
                ht["code"] = 1;
                ht["msg"] = "实体校验不合格";
                return Json(ht);
            }
            productData = HttpUtility.UrlDecode(productData);
            Product product = Newtonsoft.Json.JsonConvert.DeserializeObject<Product>(productData);
            Product oldProduct=_productService.GetModel(product.Id);
            if (oldProduct != null)
            {
                oldProduct.Price = product.Price;
            }
            _productService.Update(oldProduct);
            ht["code"] = 0;
            ht["msg"] = "success";
            return Json(ht);
        }
        //添加页面的判断商品是否存在
        public JsonResult SearchName(string keywords)
        {
            return Json(_productService.SearchName(keywords), JsonRequestBehavior.AllowGet);
        }
        //自动填充姓名
        public JsonResult SearchStaffName(string keywords)
        {
            return Json(_staffService.SearchStaffName(keywords), JsonRequestBehavior.AllowGet);
        }
        //添加出库记录
        public JsonResult OutboundInsert(string productData)
        {
            // 反序列化 JSON 字符串为 List<InboundVM> 对象
            productData = HttpUtility.UrlDecode(productData);
            List<InboundVM>  Data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<InboundVM>>(productData);
            return Json(_productService.CreateOutbound(Data), JsonRequestBehavior.AllowGet);
        } 
        //获取出入库记录
        public JsonResult InOutContent(int page, int limit, string ProductName, string ApplyName, DateTime? startTime, DateTime? endTime, int? Type)
        {
            InOutboundFilter filter = new InOutboundFilter();
            filter.page = page;
            filter.limit = limit;
            filter.IsDelete = 0;
            // 设置其他过滤条件
            filter.ProductName = ProductName;
            filter.ApplyName = ApplyName;
            filter.startTime = startTime;
            filter.endTime = endTime;
            filter.Type = Type;
            // 返回数据给前端
            return Json(_inboundService.QueryBound(filter), JsonRequestBehavior.AllowGet);
        }
        //出库审核
        [HttpPost]
        public JsonResult verify(int id)
        {
            ResMessage resMessage= _productService.ProductApi(id, ((Staff)Session["user"]).Id);
            return Json(resMessage, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}